#############################
## Custom palette function ## 
#############################

custom.palette <- function(var,col.vec,n.color=5,choose=F,style="pretty",fixedBreaks){
  
  col.ramp <- colorRampPalette(col.vec, space = "rgb")
  
  
  library(classInt)
  
  # Create palette with n color classes
  pal <- col.ramp(n=n.color) 
  
  ## (1) Show options
  if(choose==T){
    #classes_fx <- classIntervals(var, n=n.color, style="fixed", fixedBreaks=pretty(var,min.n=3,n=n.color), rtimes = 1)
    classes_pr<-classIntervals(var,n=n.color,style="pretty",rtimes=1)
    classes_sd<-classIntervals(var,n=n.color,style="sd",rtimes=1)
    classes_fi<-classIntervals(var,n=n.color,style="fisher",rtimes=3)
    classes_eq<-classIntervals(var,n=n.color,style="equal",rtimes=1)
    classes_km<-classIntervals(var,n=n.color,style="kmeans",rtimes=1)
    classes_qt<-classIntervals(var,n=n.color,style="quantile",rtimes=1)
    classes_hc<-classIntervals(var,n=n.color,style="hclust",rtimes=1)
    classes_bc<-classIntervals(var,n=n.color,style="bclust",rtimes=1)
    
    par(mar=c(2,2,2,1)+0.1, mfrow=c(2,4))
    #plot(classes_fx, pal=pal, main="Fixed Intervals", xlab="", ylab="")
    plot(classes_pr, pal=pal, main="pretty", xlab="", ylab="")
    plot(classes_sd, pal=pal, main="sd", xlab="", ylab="")
    plot(classes_fi, pal=pal, main="fisher (jenks)", xlab="", ylab="")
    plot(classes_km, pal=pal, main="kmeans", xlab="", ylab="")
    plot(classes_eq, pal=pal, main="equal", xlab="", ylab="")
    plot(classes_qt, pal=pal, main="quantile", xlab="", ylab="")
    plot(classes_hc, pal=pal, main="hclust", xlab="", ylab="")
    plot(classes_bc, pal=pal, main="bclust", xlab="", ylab="")
  }
  
  ## (2) Create cupoints
  if(choose==F){
    
    ## Define intervals
        if(style!="fixed"){classes<-classIntervals(var,n=n.color,style=style,rtimes=1)}
    if(style=="fixed"){classes <- classIntervals(var, n=n.color, style="fixed", fixedBreaks=fixedBreaks, rtimes = 1)}
    
    ## Create vector of colors
    cols <- findColours(classes, pal)
    return(cols)	
  }
}


####################################
## round.intervals                ## 
####################################


round.intervals <- function(cols,rnd){
  labs <- names(attr(cols, "table"))
  labs <- gsub("[","",labs,perl=F,fixed=T)
  labs <- gsub("]","",labs,perl=F,fixed=T)
  labs <- gsub("(","",labs,perl=F,fixed=T)
  labs <- gsub(")","",labs,perl=F,fixed=T)
  labs <- strsplit(labs,",")
  labs <- round(as.numeric(unlist(labs)),rnd)
  nn <- length(labs)/2
  labs. <- list()
  for(i in 1:nn){
    labs.[[i]] <- labs[c(2*i-1,2*i)]
    labs.[[i]] <- paste("[",paste(labs.[[i]],collapse=", "),")",sep="")
  }
  labs.[[length(labs.)]] <- gsub(")","]",labs.[[length(labs.)]],fixed=T)
  labs. <- unlist(labs.)
  return(labs.)}

#################################
##### Distance Calculation: #####
#################################

## Function: Convert km to degrees
km2d <- function(km){
  out <- (km/1.852)/60
  return(out)
}
# km2d(500) ## 500 km

## Function: Convert degrees to km
d2km <- function(d){
  out <- d*60*1.852
  return(out)
}
# d2km(1)
